﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.SharePoint.Client;

namespace TakeAPicture
{
    public partial class WebCamControl : ChildWindow
    {
        private CaptureSource captureSource = new CaptureSource();

        public WriteableBitmap Bitmap { get; set; }

        public WebCamControl()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            btnPlayCapture.Click += new RoutedEventHandler(btnPlayCapture_Click);
            btnStopCapture.Click += new RoutedEventHandler(btnStopCapture_Click);
            btnCaptureDevice.Click += new RoutedEventHandler(btnCaptureDevice_Click);
        }

        void btnCaptureDevice_Click(object sender, RoutedEventArgs e)
        {
            TryCaptureDevice();
        }

        void btnStopCapture_Click(object sender, RoutedEventArgs e)
        {
            // Stop capturing
            captureSource.Stop();

            btnPlayCapture.IsEnabled = true;
            btnStopCapture.IsEnabled = false;
        }

        void btnPlayCapture_Click(object sender, RoutedEventArgs e)
        {
            // If the device is already capturing Stop it
            if (captureSource.State == CaptureState.Started)
            {
                captureSource.Stop();
            }

            // Start capturing
            captureSource.Start();

            btnPlayCapture.IsEnabled = false;
            btnStopCapture.IsEnabled = true;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            TryCaptureDevice();
        }

        private void TryCaptureDevice()
        {
            // Get the default video capture device
            VideoCaptureDevice videoCaptureDevice = CaptureDeviceConfiguration.GetDefaultVideoCaptureDevice();

            if (videoCaptureDevice == null)
            {
                // Default video capture device is not setup
                btnPlayCapture.IsEnabled = false;
                btnStopCapture.IsEnabled = false;
                btnCaptureDevice.IsEnabled = true;

                MessageBox.Show("You don't have any default capture device");
            }
            else
            {
                btnPlayCapture.IsEnabled = false;
                btnStopCapture.IsEnabled = false;

                // Set the Capture Source to the VideoBrush of the rectangle
                VideoBrush videoBrush = new VideoBrush();
                videoBrush.SetSource(captureSource);
                rectWebCamView.Fill = videoBrush;

                // Check if the Silverlight has already access to the device or grant access from the user
                if (CaptureDeviceConfiguration.AllowedDeviceAccess || CaptureDeviceConfiguration.RequestDeviceAccess())
                {
                    btnPlayCapture.IsEnabled = true;
                    btnStopCapture.IsEnabled = false;
                    btnCaptureDevice.IsEnabled = false;
                }
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {            
            Bitmap = new WriteableBitmap(rectWebCamView, null);            
            this.DialogResult = true;
        }           

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }        
    }

    //public static class WriteableBitmapExensionMethods
    //{
    //    public static byte[] ToByteArray(this WriteableBitmap bmp)
    //    {
    //        int[] p = bmp.Pixels;
    //        int len = p.Length * 4;
    //        byte[] result = new byte[len]; // ARGB    
    //        Buffer.BlockCopy(p, 0, result, 0, len);
    //        return result;
    //    } 
    //}
}

